package com.jcxh.sg_exam.controller;

import com.alibaba.excel.EasyExcel;
import com.jcxh.sg_exam.pojo.Shicao;
import com.jcxh.sg_exam.pojo.User;
import com.jcxh.sg_exam.pojo.download.DownLoadDanxuan;
import com.jcxh.sg_exam.pojo.download.DownLoadDuoxuan;
import com.jcxh.sg_exam.pojo.download.DownLoadPanduan;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 鹏少
 * @Date 2024/8/8
 * @Date 10:55
 */
@RestController
@RequestMapping("admin/download")
@Api(tags = "模板下载接口")
public class TemplateDownLoad {

    /**
     * 人员录入模板下载
     *
     * @param response
     * @throws IOException
     */

    @ApiOperation(value = "人员录入模板下载", produces = "application/octet-stream")
    @GetMapping("/users")
    public void download(HttpServletResponse response) throws IOException {
        List<User> list = new ArrayList<>();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("人员录入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), User.class).sheet("人员录入模板").doWrite(list);
    }




    /**
     * 单选题模板下载
     *
     * @param response
     * @throws IOException
     */

    @ApiOperation(value = "单选题模板下载", produces = "application/octet-stream")
    @GetMapping("/danxuan")
    public void downloadDanXuan(HttpServletResponse response) throws IOException {
        List<DownLoadDanxuan> list = new ArrayList<>();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("单选题录入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), DownLoadDanxuan.class).sheet("单选题录入模板").doWrite(list);
    }



    /**
     * 多选题模板下载
     *
     * @param response
     * @throws IOException
     */

    @ApiOperation(value = "多选题模板下载", produces = "application/octet-stream")
    @GetMapping("/duoxuan")
    public void downloadDuoXuan(HttpServletResponse response) throws IOException {
        List<DownLoadDuoxuan> list = new ArrayList<>();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("多选题录入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), DownLoadDuoxuan.class).sheet("多选题录入模板").doWrite(list);
    }




    /**
     * 判断题模板下载
     *
     * @param response
     * @throws IOException
     */

    @ApiOperation(value = "判断题模板下载", produces = "application/octet-stream")
    @GetMapping("/panduan")
    public void downloadPanduan(HttpServletResponse response) throws IOException {
        List<DownLoadPanduan> list = new ArrayList<>();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("判断题录入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), DownLoadPanduan.class).sheet("判断题录入模板").doWrite(list);
    }



    /**
     * 实操题模板下载
     *
     * @param response
     * @throws IOException
     */

    @ApiOperation(value = "实操题模板下载", produces = "application/octet-stream")
    @GetMapping("/shicao")
    public void downloadShicao(HttpServletResponse response) throws IOException {
        List<Shicao> list = new ArrayList<>();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("实操题录入模板", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Shicao.class).sheet("实操题录入模板").doWrite(list);
    }




}
